var datas = [];
var isUpdate = false;//是否修改，默认不是
var currentRow;
var search_data = {page:1,limit:5,
    type:stitle.value,
    timerule:stitle.value};

loadData();

function showData(arr){
    list_div.innerHTML = "";
    if(arr.length==0){
        list_div.innerHTML = "暂无数据";
        return;
    }

    var allhtml = "";
    allhtml += "<table cellpadding='0' cellspacing='0'" +
        " width='100%'><tr class='theader'>" +
        "<th>超期类型</th><th>超期时间规则</th><th>创建时间</th>" +
        "<th>修改时间</th><th>操作</th></tr>";
    for(var i=arr.length-1;i>=0;i--){
        var a = arr[i];

        allhtml +=
            "<tr class='list'>" +
            "<td class='ntitle'>"+a.type+"</td>" +
            "<td class='ntitle'>"+a.timerule+"</td>" +
            "<td class='ntitle'>"+a.createtime.substring(0,10)+"</td>" +
            "<td class='ntitle'>"+a.modifytime.substring(0,10)+"</td>" +
            "<td class='ntitle'><a class='edit_a' onclick='upd("+i+")' href='javascript:;'>编辑</a>" +
            "<a class='del_a' onclick='del("+a.id+")' href='javascript:;'>删除</a></td>" +
            "</tr>";
    }
    allhtml += "</table>";
    list_div.innerHTML =allhtml;


}
function search(){
       search_data = {page:1,limit:5,
           type:stitle.value,
           timerule:stitle.value}
       loadData();
}


function save(){
    if(!ztype.value){
      type_error.innerHTML = "请您填写超期类型";
      return;
    }

    ajax(isUpdate?"/upd_chaoqi":"/add_chaoqi",{
        id:currentRow?currentRow.id:-1,
        type:ztype.value,
       timerule:timerule.value,
       createtime:createtime.value,
        modifytime:modifytime.value,
    },function(data){
        alert(isUpdate?"更新成功":"新增成功！");
        loadData();
        add_div.style.display = "none";
        list_div.style.display = "block";
        save_btn.style.display = "none";
        add_btn.style.display = "block";
        return_btn.style.display = "none";
        search_div.style.display = "block";
    })

}

function upd(i){
    isUpdate=true;
    var d = datas[i];
    currentRow = d;
    add_div.style.display = "block";
    list_div.style.display = "none";
    save_btn.style.display = "block";
    add_btn.style.display = "none";
    search_div.style.display = "none";
    return_btn.style.display = "block";

    type.value = d.ztype;
   timerule.value = d.timerule;
    createtime.value = d.createtime.substring(0,10);
    modifytime.value = d.modifytime.substring(0,10);

    type.focus();
}

function del(id){
    if(!confirm("您确定要删除这个数据吗？")){
        return;
    }
    var req = new XMLHttpRequest();
    req.open("post","/del_chaoqi");
    req.setRequestHeader("content-Type","application/json;charset=utf-8");
    var data = {
        id:id
    }
    req.send(JSON.stringify(data));
    req.onreadystatechange = function(){
        if(req.readyState==4&&req.status==200){
            if(req.responseText.trim()=="1"){
                alert("删除成功！");
            }

            loadData();
        }
    }
}

function add(){
    isUpdate = false;
    add_div.style.display = "block";
    list_div.style.display = "none";
    save_btn.style.display = "block";
    add_btn.style.display = "none";
    search_div.style.display = "none";
    return_btn.style.display = "block";

   type.value = "";
    timerule.value = "";
    modifytime.value = "";
  createtime.value = getNow();
   type.focus();
}

function returnMain(){
    add_div.style.display = "none";
    list_div.style.display = "block";
    save_btn.style.display = "none";
    add_btn.style.display = "block";
    search_div.style.display = "block";
    return_btn.style.display = "none";
}

function loadData(data){
//    list_div.innerHTML = "";
//    loading.style.display = "block";
//    var req = new XMLHttpRequest();
//    req.open("post","/find_all_pay");
//    req.setRequestHeader("content-Type","application/json;charset=utf-8");
//    if(!data) data = {}
//    req.send(JSON.stringify(data));
//    req.onreadystatechange = function(){
//        if(req.readyState==4&&req.status==200){
//            pays = JSON.parse(req.responseText);
//            showData(pays);
//            loading.style.display = "none";
//        }
//    }
    list_div.innerHTML = "";
    loading.style.display = "block";
    ajax("find_all_chaoqi",search_data,function(data){
         datas = data;
         showData(datas);
         loading.style.display = "none";
    });

    ajax("count_all_chaoqi",search_data,function(data){
       // console.log(data);  33
        data = parseInt(data);
        current_page.innerHTML = search_data.page;
        total_page.innerHTML =
            (data%search_data.limit==0?
                data/search_data.limit:
                Math.floor(data/search_data.limit)+1);
    });
}

function getNow(){
    var now = new Date();
    var y = now.getFullYear();
    var m = now.getMonth()+1;
    var d = now.getDate();
    if(m<10) m = "0"+m;
    if(d<10) d = "0"+d;
    return y+"-"+m+"-"+d;
}


function last_page(){
    if(search_data.page>1) {
        search_data.page --;
        loadData();
    }
}

function next_page(){
    if(search_data.page<parseInt(total_page.innerHTML)) {
        search_data.page++;
        loadData();
    }
}

function to_page(){
    var ip = parseInt(input_page.value);
    if(ip>=1&&ip<=parseInt(total_page.innerHTML)){
        search_data.page = ip;
        loadData();
    }
}

function first_page(){
     search_data.page = 1;
     loadData();
}

function ultimate_page(){
    search_data.page = parseInt(total_page.innerHTML);
    loadData();
}
